package com.moleq.dialog;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.moleq.adapter.SelectionAdapter;
import com.moleq.base.MoleQBaseDialog;
import com.moleq.logic.MoleQConstants;
import com.moleq.logic.TransactionManager;
import com.moleq.mgpos.activities.R;
import com.moleq.models.AdapterDataSourceBuilder;
import com.moleq.models.SubDepartmant;
import com.moleq.service.ServiceManager;

public class GPCDeptSelectionDialog extends MoleQBaseDialog {

	private Button mButtonBack,mButtonOk;
	private TransactionManager mPaymentMgr;
	private Context mContext;
	private ListView mListView;
	private List<SubDepartmant> mDataSource = new  ArrayList<SubDepartmant>();
	private SelectionAdapter mAdapter;
	private int selectedRow = 0;
//	private static final int DIALOG_PRICE_DEPT_INFO = 1;
	
	private Button.OnClickListener mOnClickListener = new Button.OnClickListener() {

		@Override
		public void onClick(View v) {	
			switch(v.getId()){
			case R.id.btn_dialog_cancel:
				dismiss();
				break;
			case R.id.btn_dialog_ok:	
				if(mDataSource.size() <= 0){
					return;	
				}
				onCloseWithReturn(mDataSource.get(selectedRow));
				dismiss();
				break;
			}
		}
	};
	
	public ListView.OnItemClickListener onItemClickListener = new ListView.OnItemClickListener() {


		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {

			switch (parent.getId()) {
			case R.id.lv_dialog_dept_list:
				ScrollToRow(position);
				break;
			}
		}
	};
	
	
	public void onCloseWithReturn(Object obj){
		onReturnValue(MoleQConstants.DIALOG_PRICE_DEPT_INFO,obj);
		dismiss();
	}
	
	public GPCDeptSelectionDialog(Context context) {
		super(context,context);
		mContext = context;
		
	}
	public GPCDeptSelectionDialog(Context context, Object base) {
		super(context,base);
		mContext = context;
	}

	public GPCDeptSelectionDialog(Context context, int theme) {
		super(context, theme);
		mContext = context;
	}
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// set UI layout
		this.setContentView(R.layout.price_dept_dialog_perview_models);
		this.setCancelable(false);
	
		mButtonBack = (Button) findViewById(R.id.btn_dialog_cancel);
		mButtonBack.setOnClickListener(mOnClickListener);
		
		mButtonOk = (Button) findViewById(R.id.btn_dialog_ok);
		mButtonOk.setOnClickListener(mOnClickListener);

		mListView = (ListView) findViewById(R.id.lv_dialog_dept_list);
		mListView.setOnItemClickListener(onItemClickListener);
		mDataSource =  ServiceManager.getInstance().mSubDepartmantService.FindAll();
		mAdapter = new SelectionAdapter(mContext,AdapterDataSourceBuilder.readValueFromSubDep(mDataSource));
		mListView.setAdapter(mAdapter);
	}
	
	public void show(){
		super.show();		
		mPaymentMgr =  TransactionManager.getInstance();
		
		if (mDataSource.size() <= 0) {
			SubDepartmant dep = new SubDepartmant();
			dep.setSd_name("Department1");
			dep.setSd_id(1);
			mDataSource.add(dep);
		}else{
			mAdapter.ScrollToRow(0);
		}
		mAdapter.notifyDataSetChanged();
	}
	
	public void ScrollToRow(int row){
		selectedRow = row;
		mAdapter.ScrollToRow(row);
		mAdapter.notifyDataSetChanged();
	}

}
